package top.fangw.tenant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.seata.common.util.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import top.fangw.common.core.exception.BizException;
import top.fangw.tenant.mapper.SysDictDataMapper;
import top.fangw.tenant.pojo.dto.SysDictTypeDto;
import top.fangw.tenant.pojo.entity.SysDictData;
import top.fangw.tenant.pojo.entity.SysDictType;
import top.fangw.tenant.mapper.SysDictTypeMapper;
import top.fangw.tenant.pojo.vo.DictTypeVo;
import top.fangw.tenant.service.ISysDictTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 用途：用于存放字典的类型值 服务实现类
 * </p>
 *
 * @author fangw
 * @since 2024-02-23
 */
@Service
@RequiredArgsConstructor
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements ISysDictTypeService {

    private final SysDictDataMapper sysDictDataMapper;

    @Override
    public void addDictType(SysDictTypeDto dto) {

        if(dto != null){
            Long aLong = this.baseMapper.selectCount(new LambdaQueryWrapper<SysDictType>().eq(SysDictType::getType, dto.getType()));
            if(aLong != null && aLong > 0){
                throw new BizException("字典类型type已经存在，请重新录入");
            }
            SysDictType sysDictType = new SysDictType();
            BeanUtils.copyProperties(dto,sysDictType);
            sysDictType.setCreatedByName("MYH");
            sysDictType.setCreatedBy("MYH");
            sysDictType.setCreatedAt(LocalDateTime.now());
            this.baseMapper.insert(sysDictType);
        }
    }

    @Override
    public List<DictTypeVo> getDictTypeList(String name) {
        return this.baseMapper.getDictTypeList(name);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDictType(String id) {
        SysDictType sysDictType = this.baseMapper.selectById(id);
        if(sysDictType != null){
            if("0".equals(sysDictType.getIsDeletable())){
                throw new BizException("字典类型状态为不可删除");
            }else{
                //判断字典数据  是否存在不可删除数据
                Long cnt = sysDictDataMapper.selectCount(new LambdaQueryWrapper<SysDictData>()
                        .eq(SysDictData::getDictTypeId, sysDictType.getId()).eq(SysDictData::getIsDeletable, "0"));
                if(cnt != null && cnt > 0){
                    throw new BizException("该字典类型下存在状态为不可删除的字典数据");
                }
                this.baseMapper.deleteById(id);
                sysDictDataMapper.delete(new LambdaQueryWrapper<SysDictData>().eq(SysDictData::getDictTypeId,id));
            }
        }
    }

    @Override
    public void updateDictType(SysDictTypeDto dto) {
        SysDictType exist = this.getByIdAndType(dto.getId(), dto.getType());
        if(exist != null){
            throw new BizException("该字典类型已经存在，请重新修改字典类型");
        }
        SysDictType sysDictType = new SysDictType();
        BeanUtils.copyProperties(dto,sysDictType);
        this.baseMapper.updateById(sysDictType);
    }

    public SysDictType getByIdAndType(String id,String type){
        return this.baseMapper.selectOne(new LambdaQueryWrapper<SysDictType>()
                .eq(SysDictType::getType,type).ne(SysDictType::getId,id));
    }
}
